package light.head.controller.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.oa.AuditStatusDAO;
import light.head.dao.oa.LeaveDAO;
import light.head.dao.oa.ProcessConfigDAO;
import light.head.dao.sys.MessageDAO;
import light.head.dao.sys.MessageTemplateDAO;
import light.head.dao.sys.OrganDAO;
import light.head.dao.sys.UserInfoDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.AuditStatus;
import light.head.model.LeaveManage;
import light.head.model.ProcessConfig;
import light.head.model.UserInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping(value = Url.C_LEAVEAPPLY)
public class LeaveApplyController extends SupporterController {
	
	private LeaveDAO leaveDAO;
	private UserInfoDAO userDAO;
	private AuditStatusDAO auDAO;
	private MessageTemplateDAO mtDAO;
	private MessageDAO msgDAO;
	private ProcessConfigDAO pcDAO;
	private OrganDAO organDAO;
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.LEAVEAPPLY_INIT);
	}

	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.CU, Parm.ST, Parm.END, Parm.AGREE, Parm.REASON, Parm.OC, Parm.DELETED);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (leaveDAO.findByMap_1(cu_code(request), organDAO.getSonCodeList(oc), map, pb) == RESULT.SUCCESS) {
			LeaveManage leave = null;
			for (Object o : pb.getResultList()) {
				leave = (LeaveManage) o;
				if (leave.getHead() == false){
					leave.setAuditStatusList(auDAO.findByBusinessMc(Sys.B_TWO, leave.getCode()));
				}else{
					leave.setAuditStatusList(auDAO.findByBusinessMc(Sys.B_SIX, leave.getCode()));
				}
			}
			
		}
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.LEAVEAPPLY_LIST);
	}
	
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			LeaveManage leave = leaveDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.LEAVE, leave);
		}
		return new ModelAndView(Url.LEAVEAPPLY_EDIT);
	}

	@Transactional
	@RequestMapping(value = Url.SAVE)
	public void save(LeaveManage leave, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		Integer oc = cu_organCode(request);
		// 检查是否配置审核流程
		List<ProcessConfig> pcList = new ArrayList<ProcessConfig>();
		//判断是否是负责人提交的申请
		if(leave.getHead() == true){
			 pcList = pcDAO.findByOcBusiness(organDAO.getRoot(cu_organCode(request)).getCode(), Sys.B_SIX);
		}else{
			 pcList = pcDAO.findByOcBusiness(organDAO.getRoot(cu_organCode(request)).getCode(), Sys.B_TWO);
		}
		if(pcList == null || pcList.size() == 0){
			print(response, Msg.NOT_FIND);
			return;
		}
/*		//未审核时修改
		if (leave.getCode() != null && leave.getPass().byteValue() == Sys.B_ONE) {
			LeaveManage old = leaveDAO.get(leave.getCode());
			old.setReason(leave.getReason());
			old.setEnd(leave.getEnd());
			old.setStart(leave.getStart());
			leaveDAO.add(old);
		// 审核不通过修改
		} else if(leave.getCode() != null && leave.getPass().byteValue() == Sys.B_TWO) {
			LeaveManage old = leaveDAO.get(leave.getCode());
			//删除以前审核状态，重修提交申请
			List<AuditStatus> auList = auDAO.findByBusinessMc(Sys.B_TWO, leave.getCode());
			for (AuditStatus auditStatus : auList) {
				auDAO.del(auditStatus);
			}
			int i = 1;
			for (ProcessConfig pc : pcList) {
				AuditStatus au = new AuditStatus();
				au.setBusiness(Sys.B_FOUR);
				au.setCt(new Date());
				au.setCu(cu);
				au.setOc(oc);
				au.setMc(leave.getCode());
				au.setRc(pc.getRc());
				au.setPcc(pc.getCode());
				au.setPc(pc.getPc());
				// 默认第一个审核节点的上一节点审核状态为1通过
				if (null != pc.getPc() && pc.getPc().intValue() == 1) {
					au.setBeforeStatus(Sys.B_ONE);
				}
				if (null != pc.getPc() && pc.getPc().intValue() == 1 && null != rc1 && pc.getRc() == rc1){
					au.setStatus(Sys.B_ONE);
					au.setAt(new Date());
					au.setAu(cu);
					i++;
				}
				if (null != pc.getPc() && pc.getPc().intValue() == 2 && i == 2) {
					au.setBeforeStatus(Sys.B_ONE);
				}
				result = auDAO.add(au);
			}
			// 消息通知审核人
			Integer rc = auDAO.findByBusinessOcMc(Sys.B_TWO, oc, leave.getCode());	// 获取该机构具有审核申请单的角色编号
			if (null != rc) {
				List<UserInfo> userList = userDAO.findByRoleRc(oc, rc);	// 获取有审核角色的所有用户
				List<Integer> rucs = new ArrayList<Integer>();
				for (UserInfo u : userList) {
					rucs.add(u.getCode());
				}
				String content = mtDAO.leaverTofirst(old, cu_realName(request));
				msgDAO.sendToUsers("【" + cu_realName(request) + "】提交申请待审核通知", content, cu_code(request), cu_organCode(request), rucs);
			}
		} else if(leave.getCode() == null) {*/
			leave.setDeleted(false);
			leave.setCu(cu_code(request));
			leave.setOc(cu_organCode(request));
			leave.setCt(new Date());
			result = leaveDAO.add(leave);
			
			int i = 1;
			for (ProcessConfig pc : pcList) {
				AuditStatus au = new AuditStatus();
				if(leave.getHead() == false){
					au.setBusiness(Sys.B_TWO);
				}else{
					au.setBusiness(Sys.B_SIX);
				}
				au.setCt(new Date());
				au.setCu(cu_code(request));
				au.setOc(cu_organCode(request));
				au.setMc(leave.getCode());
				au.setRc(pc.getRc());
				au.setPcc(pc.getCode());
				au.setPc(pc.getPc());
				// 默认第一个审核节点的上一节点审核状态为1通过
				if (null != pc.getPc() && pc.getPc().intValue() == 1){
					au.setBeforeStatus(Sys.B_ONE);
				}
				if (null != pc.getPc() && pc.getPc().intValue() == 1 && null != cu_roleCode(request) && pc.getRc() == cu_roleCode(request)){
					au.setStatus(Sys.B_ONE);
					au.setAt(new Date());
					au.setAu(cu_code(request));
					i++;
				}
				if(null != pc.getPc() && pc.getPc().intValue() == 2 && i == 2){
					au.setBeforeStatus(Sys.B_ONE);
				}
				result = auDAO.add(au);
				// 消息通知审核人
				Integer rc;
				if(leave.getHead() == false){
					 rc = auDAO.findByBusinessOcMc(Sys.B_TWO, oc, leave.getCode());	// 获取该机构具有审核申请单的角色编号
				} else{
					 rc = auDAO.findByBusinessOcMc(Sys.B_SIX, oc, leave.getCode());	// 获取该机构具有审核申请单的角色编号
				}
				if (null != rc) {
					List<UserInfo> userList = userDAO.findByRoleRc(cu_organCode(request), rc);	// 获取有审核角色的所有用户
					List<Integer> rucs = new ArrayList<Integer>();
					for (UserInfo u : userList) {
						rucs.add(u.getCode());
					}
					String content = mtDAO.leaverTofirst(leave, cu_realName(request));
					msgDAO.sendToUsers("【" + cu_realName(request) + "】提交申请待审核通知", content, cu_code(request), cu_organCode(request), rucs);
				} 
			}
		if(result == RESULT.SUCCESS){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
			return;
		}
	}
	
	@RequestMapping(value = Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		LeaveManage leave = leaveDAO.get(Integer.parseInt(code));
		if (leave == null) {
			print(response, Msg.NOT_FIND);
		} else {
			leave.setDeleted(true);
			if (leaveDAO.upd(leave) == RESULT.SUCCESS) {
				print(response, Msg.SUCCESS);
			} else {
				print(response, Msg.ERROR);
			}
		}
	}
	
	
	@RequestMapping(value = Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		LeaveManage leave = leaveDAO.get(Integer.parseInt(code));
		if (leave == null) {
			print(response, Msg.NOT_FIND);
		} else {
			leave.setDeleted(false);
			if (leaveDAO.upd(leave) == RESULT.SUCCESS) {
				print(response, Msg.SUCCESS);
			} else 
				print(response, Msg.ERROR);
		}
	}
	

	@Autowired
	public void setLeaveDAO(LeaveDAO leaveDAO) {
		this.leaveDAO = leaveDAO;
	}
	@Autowired
	public void setUserDAO(UserInfoDAO userDAO) {
		this.userDAO = userDAO;
	}
	@Autowired
	public void setAuDAO(AuditStatusDAO auDAO) {
		this.auDAO = auDAO;
	}
	@Autowired
	public void setMtDAO(MessageTemplateDAO mtDAO) {
		this.mtDAO = mtDAO;
	}
	@Autowired
	public void setMsgDAO(MessageDAO msgDAO) {
		this.msgDAO = msgDAO;
	}

	@Autowired
	public void setPcDAO(ProcessConfigDAO pcDAO) {
		this.pcDAO = pcDAO;
	}

	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}


	
}
